package com.example.myapplication.ui.dashboard;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import android.app.Fragment;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.myapplication.BookDetailActivity;
import com.example.myapplication.R;
import com.example.myapplication.RegisterActivity;
import com.example.myapplication.dao.FindorSort;
import com.example.myapplication.entity.Book;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.SimpleTimeZone;

public class SortFragment extends Fragment {

    public static Book storedBook;
    public static Bitmap storedPic;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.frgment_sort, container, false);

        ListView listView = rootView.findViewById(R.id.card);
        ArrayList ListItems = new ArrayList<HashMap<String, Object>>();

        FindorSort findorSort = new FindorSort();
        Handler handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                HashMap<String, Object> map;
                if (msg.what == 2) {
                    int size = msg.arg1;
                    for (int i = 0; i < size; i++) {
                        map = new HashMap<>();
                        map.put("img", findorSort.picarrayList.get(i));
                        map.put("author", findorSort.arrayList.get(i).getAuthor());
                        map.put("name", findorSort.arrayList.get(i).getName());map.put("detail", findorSort.arrayList.get(i).getDetail());
                        ListItems.add(map);
                    }
                    SimpleAdapter adapter = null;
                    if (getContext() != null && ListItems != null) {
                        adapter = new SimpleAdapter(getContext(), ListItems, R.layout.card_view, new String[]{"img", "author", "name","detail"}, new int[]{R.id.novel_image1, R.id.novel_author1, R.id.novel_name1,R.id.novel_detail1});
                        //接管SimpleAdapter的图像处理

                        adapter.setViewBinder((view, data, textRepresentation) -> {
                            if ((view instanceof ImageView) & (data instanceof Bitmap)) {
                                ImageView imageView = (ImageView) view;
                                Bitmap mBitmap = (Bitmap) data;
                                imageView.setImageBitmap(mBitmap);
                                return true;
                            }
                            return false;
                        });
                        listView.setAdapter(adapter);
                    }
                }
            }
        };

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // HashMap<String,Object> item=(HashMap<String, Object>) parent.getItemAtPosition(position);

                Intent myintent = new Intent(getActivity().getApplicationContext(), BookDetailActivity.class);
                storedBook = findorSort.arrayList.get(position);
                storedPic = (findorSort.picarrayList.get(position));
                startActivity(myintent);

            }
        });
        if (this.getArguments().getString("sort") != null)
            findorSort.getBooksByFindorSort("sort", this.getArguments().getString("sort"), handler);
        else if (this.getArguments().getString("find") != null) {
            findorSort.getBooksByFindorSort("find", this.getArguments().getString("find"), handler);
            System.out.println(this.getArguments().getString("find"));
        }
        return rootView;

    }
}